﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HeapModel;
using HeapModel.ProgInfo;
using HeapModel.RegionPartition;

namespace MetricExtractor
{
	internal static class ExtractionSupport
	{
		public static bool SortOutput = false;

		private static string[] s_immutableNames = { "System.String" };

		internal static bool IsTypeImmutable(TypeIdentifier ttid)
		{
			string ttname = ttid.NameValue;
			return s_immutableNames.Contains(ttname);
		}

		internal static bool AllTypesImmutable(IEnumerable<TypeIdentifier> ttide)
		{ return ttide.All((TypeIdentifier ttid) => IsTypeImmutable(ttid)); }

		internal static bool IsNodeAnOwnedRegion(RegionNode hr, List<RegionEdgeInfo> prededges)
		{
			if(prededges.Count > 1 || hr.ExtraFacts.Shape != RegionShapeEnum.Atomic)
				return false;

			return prededges.Count == 0 || prededges.First().IsInjective;
		}

		internal static string FormatDoubleListForPrettyPrint(IEnumerable<double> dl)
		{
			StringBuilder sb = new StringBuilder();
			double[] sra = dl.ToArray();

			if(SortOutput)
			{
				Array.Sort(sra);
				Array.Reverse(sra);
			}

			for(int i = 0; i < sra.Length; ++i)
				sb.Append((i != 0 ? ", " : "") + String.Format("{0:F3}", sra[i]));

			return sb.ToString();
		}

		internal static string FormatDoubleListForCSV(IEnumerable<double> dl)
		{
			StringBuilder sb = new StringBuilder();
			double[] sra = dl.ToArray();

			if(SortOutput)
			{
				Array.Sort(sra);
				Array.Reverse(sra);
			}

			for(int i = 0; i < sra.Length; ++i)
				sb.Append((i != 0 ? ", " : "") + String.Format("{0:F3}", sra[i]));

			return sb.ToString();
		}
	}
}
